package org.example.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.dto.DepartmentDto;
import org.example.service.DepartmentService;
import org.example.service.TestService;
import org.example.utils.JsonUtils;
import org.example.vo.DepartmentVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Rongchang Wu
 * @version 1.0
 * @description: TODO
 * @date 2021/12/7 19:06
 */
@Controller
public class DepartmentController {
    @Resource
    DepartmentService service;

    @GetMapping("/getDepartmentByName")
    @ResponseBody
    public void getDepartmentByName(HttpServletResponse resp, String departmentName) throws IOException {
        DepartmentDto department = service.queryDepartmentByName(departmentName);
        //以上service根据系别名查找，返回对应的系
        List<DepartmentDto> departmentList = new ArrayList<>();
        departmentList.add(department);

        JsonUtils.customJson(resp, departmentList, departmentList.size());
    }

    @GetMapping("/getDepartmentList")
    @ResponseBody
    public void getDepartmentList(HttpServletResponse resp, Integer page, Integer limit) throws IOException {
        Page<Object> obj = PageHelper.startPage(page, limit);
        List<DepartmentDto> departmentList = service.queryAllDepartment();
        Integer count = service.countDepartment();
        //以上调用service查找所有系别，赋值给departmentList；查找系别数量，赋值给count

        JsonUtils.customJson(resp, departmentList, count);
    }

    @GetMapping(value = "/deleteDepartment", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String deleteDepartment(String departmentId) {
        String[] departmentIds = departmentId.split(",");
        Integer status = 0;
        for (int i = 0; i < departmentIds.length; i++) {
            status += service.deleteDepartment(Integer.parseInt(departmentIds[i]));
        }
        //以上调用service方法删除系别

        return status > 0 ? "success" : "error";
    }

    @GetMapping(value = "/addDepartment", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String addDepartment(DepartmentVo department) {
        Integer status = service.addDepartment(department);
        //以上调用service方法添加系别

        return status > 0 ? "success" : "error";
    }

    @GetMapping("/getDepartmentById")
    @ResponseBody
    public DepartmentDto getDepartmentById(Integer departmentId) {
        DepartmentDto department = service.queryDepartmentById(departmentId);
        //以上service根据系系号查找，返回对应的系

        return department;
    }

    @GetMapping("/editDepartment")
    @ResponseBody
    public String editDepartment(DepartmentVo department) {
        Integer status = service.updateDepartment(department);
        //以上调用service方法修改系别

        return status > 0 ? "success" : "error";
    }
}
